package top.lywivan.complexity;

/**
 * @author :lyw.ivan
 * @date :Created in 2022/5/24
 * @description :
 * @version: 1.0
 */
public class ComplexityAnalysis {

    /**
     * 计算1-n的累加，假设每句代码执行时间为timer，则总执行时间为T(n)=(3n+3)*timer
     * 时间复杂度O(n)
     * @param n
     * @return
     */
    public int sum(int n){
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum = sum+i;
        }
        return sum;
    }

    /**
     * 假设每句代码执行时间为timer，则总执行时间为T(n)=(3n^2+3n+3)*timer
     * 时间复杂度O(n^2)
     * @param n
     * @return
     */
    public int sum2(int n){
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                sum = sum+i*j;
            }
        }
        return sum;
    }
}
